<?php
	
require '../include/user_login.inc.php';
require '../include/paging.inc.php';

	
	
/*if($_SESSION['ACCESS_SID'] != 2) redirect(HTML_BASE.$_SESSION["lang"].'/login/');
*/
$redirect = $_SERVER['HTTP_REFERER'];	
$user_id = $_SESSION['USER_SID'];
$u_id = $_GET["nmr"];

if ($_POST['is_submitted']) {
		$error_message = '';
		
		if (!$_POST['title']) $error_message .= 'Не е въведенa Тема<br/>';
		if (!$error_message) {
			$sql = "INSERT INTO tasks
						 (title,priority,status,by_user)
					   VALUES
						 ('".escape($_POST['title'])."','".$_POST['priority']."','1','".$u_id."')
					   
					";

				query($sql);
				$id = mysql_insert_id();
				redirect("task.php?task_id=".$id);
				
			
		}
	}
	

if($_POST["is_note_sub"])
{
	$error_message = '';
		
		if (!$_POST['title']) $error_message .= 'Не е въведенa Тема<br/>';
		
		
		if (!$error_message) {
			$sql = "INSERT INTO notes
						 (title,priority,status,by_user,description, 	end_date)
					   VALUES
						 ('".escape($_POST['title'])."','".$_POST['priority']."','1','".$u_id."''".escape($_POST['title'])."','".$_POST['description']."','".$_POST['creation_datetime']."')
					   
					";

				query($sql);
				$id = mysql_insert_id();
				//redirect("note.php?note_id=".$id);
				
			
		}
}

	
$sql = "SELECT
			t.task_id,
			t.title,
			t.priority,
			t.status,
			t.by_user,
			t.creation_time
		FROM
			tasks AS t,
			law_users AS l
		WHERE
			l.law_id = '$user_id'
			AND
			t.by_user = '$u_id'
			AND
			l.use_id = t.by_user
		
			
		";


if (!isset($_GET['sort'])) $_GET['sort'] = 1;
if (!isset($_GET['sortBy'])) $_GET['sortBy'] = 6;

$paging = new Paging($sql, $_GET['sortBy'], $_GET['sort'], array(6=>"Подредба"), 30);
$result = $paging->query();
	
	head('User history');
	if($_SESSION['ACCESS_SID'] == 2) menu_law();

?>

<div id="content">

<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>js/calendarDateInput.js"></script>
<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	// General options
	language : "bg", 
	mode : "exact",
    elements : "body1",
	theme : "advanced",
	relative_urls : false,
	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",

	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,|,image,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : false,
	
});
</script>
			
			<?php if ($_SESSION['message']) { ?>
			<div class="message green">
				<?=$_SESSION['message']?>
			</div>
			<?php unset($_SESSION['message']); } ?>
            <?php if ($error_message) { ?>
			<div class="red" style="padding:10px;">
				<?=$error_message?>
			</div><br /><br />
			<?php } ?>
			
	<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            <input type="hidden" name="user_id" value="<?=$u_id?>" />
			 
              <dl class="clearfix" style="border:1px solid #ccc; background:#FFFFD5; border-radius:10px;">
              <h4>Нова кореспонденция с <?=get_user_name($u_id)?></h4>
              <dl style="float:left; border:none; background:none;">
            <dt><label for="priority" class="required">Приоритет</label></dt>
            <dd>
              <input type="radio" name="priority" value="1" checked="checked">Основен
			  <input  type="radio" name="priority" value="2">Среден
              <input type="radio" name="priority" value="3">Спешен
              </dd>
             </dl>
              <dl style="float:left; border:none; background:none;">
              <dt><label for="title" class="required">Тема/Заглавие</label></dt>
            <dd class="short"><input class="inputbox" name="title" type="text" ></dd>
            
            
            </dl>
            <dl style="float:left; border:none; background:none; margin:20px 0;">
                  <input type="submit" name="submit" id="submit" value="Старт">
         	</dl>
                
              </dl>
			</form>

            <h3>Хронология (<?=$paging->count() ?>) кореспонденции  с <?=get_user_name($u_id)?></h3>
            
            <table id="listing" width="100%" cellpadding="0" cellspacing="0">
			<thead>
				<tr>
                <td>Приоритет</td>
					<td>Тема</td>
					<td>Дата</td>
                    
					
				</tr>
			</thead>
			<tbody>
			<?php
			  
			  
			  $i=1;
			  while ($row = mysql_fetch_object($result)) {				  
			?>
					<tr class="<?=(($i%2)?"none":"zebra")?>">
						
						
                       <?php   if($row->priority == 1) $c= "#E1FFE1";
								if($row->priority == 2) $c= "#FF9";
								if($row->priority == 3) $c= "#F66";
								
						?>
						<td class="switch" style=" background:<?=$c?>">
                        <?php   if($row->priority == 1) echo "Основен";
								if($row->priority == 2) echo "Среден";
								if($row->priority == 3) echo "Спешен";
								
						?>
							
						</td>
						<td><a href="../task.php?task_id=<?=$row->task_id?>" title="Преглед"><?=sth($row->title)?></a></td>
              
						<td class="switch">
							<?=$row->creation_time?>	
						</td>
						
						
					</tr>
         <?php
              $i++;    
            } 
         ?>				  
			</tbody>
		</table>
        
        <div class="pagingwrap"><div class="paging">
			<?=$paging->display()?>
		</div></div>
         <hr />  
           <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_note_sub" value="1" />
            <input type="hidden" name="user_id" value="<?=$u_id?>" />
           
        

            <dl class="clearfix">
            <h4>Нова задача за <?=get_user_name($u_id)?></h4>
             <dl style="float:left; border:none; background:none;">
              <dt><label for="title" class="required">Тема/Заглавие</label></dt>
              
            <dd class="short"><input class="inputbox" name="title" type="text" ></dd>
            
            </dl>
            <dl style="float:left; border:none; background:none;">
            <dt><label for="priority" class="required">Приоритет</label></dt>
            <dd>
              <input type="radio" name="priority" value="1" checked="checked">Основен
			  <input  type="radio" name="priority" value="2">Среден
              <input type="radio" name="priority" value="3">Спешен
              </dd></dl>
              
              
              
              <dt><label>Дата</label></dt>
			<dd class="date"><script>DateInput('creation_datetime',true,'YYYY-MM-DD',
			'<?=(($_POST['creation_datetime'])?date('Y-m-d',strtotime($_POST['creation_datetime'])):date('Y-m-d'))?>')</script></dd>
              
              
              
            
            <dt style="clear:both;"><label>Коментар</label></dt>
            <dd><textarea id="body1"  class="inputbox" style="width:100%" name="description" rows="10"><?=sth($_POST["description"])?></textarea></dd>
            
            <dd class="submit">
            <input type="submit" name="submit" id="submit" value="запиши">
            </dd>
			
			</dl>			
			</form> 
            
<?php
		
	$sql = "SELECT
				n.note_id,
				n.title,
				n.priority,
				n.status,
				n.by_user,
				n.start_date,
				n.end_date
			FROM
				notes AS n,
				law_users AS l
			WHERE
				l.law_id = '$user_id'
				AND
				n.by_user = '$u_id'
				AND
				l.use_id = n.by_user
			
			ORDER BY n.start_date	DESC
			";
 
			
			
			$result = query($sql);
			
			?>  
            
                        <h3> <?=get_user_name($u_id)?> - задачи</h3>
            
           
			<?php
			  
			  
			  while ($row = mysql_fetch_object($result)) {				  
			?>
            	
                       <?php   if($row->priority == 1) $c= "#E1FFE1";
								if($row->priority == 2) $c= "#FF9";
								if($row->priority == 3) $c= "#F66";
								
						?>
						<a href="../note.php?note_id=<?=$row->note_id?>" title="Преглед">
						<div style=" float:left; width:160px; height:160px; background:
                        <?=$c?>; border:#999 2px solid; margin:3px; padding:5px;">
					
						
                        <?php   /*if($row->priority == 1) echo "Основен";
								if($row->priority == 2) echo "Среден";
								if($row->priority == 3) echo "Спешен";*/
								
						?>
							
						<h4 style="background:#FFF;"><?=sth($row->title)?></h4>
              
						
							<?php
                            $date = date_create($row->end_date);
							echo date_format($date, 'j M Y');
							
							?>	
						</div></a>
         <?php
                 
            } 
         ?>		
			
			
   </div>
    

<?php
foot();
?>